package com.yowasa.xiuxian.engine.message.map;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.yowasa.xiuxian.annoations.CMD;
import com.yowasa.xiuxian.engine.BattleEngine;
import com.yowasa.xiuxian.engine.message.CmdMessageHandler;
import com.yowasa.xiuxian.entity.bo.battle.BattleContent;
import com.yowasa.xiuxian.entity.bo.enemy.EnemyBO;
import com.yowasa.xiuxian.entity.bo.user.UserBO;
import com.yowasa.xiuxian.entity.enums.CmdScopeEnum;
import com.yowasa.xiuxian.entity.enums.DungeonEnum;
import com.yowasa.xiuxian.entity.mys.SendMessage;
import com.yowasa.xiuxian.entity.po.XiuXianExtend;
import com.yowasa.xiuxian.exception.HandlerException;
import com.yowasa.xiuxian.service.helper.*;
import com.yowasa.xiuxian.util.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

@CMD(name = "/副本", scope = CmdScopeEnum.ALL)
public class DungeonHandler  extends CmdMessageHandler {
    @Autowired
    private UserHelper userHelper;
    @Autowired
    private MessageHelper messageHelper;
    @Autowired
    private EnemyHelper enemyHelper;
    @Autowired
    private BattleEngine battleEngine;
    @Autowired
    private BattleHelper battleHelper;
    @Autowired
    private ItemHelper itemHelper;
    @Autowired
    private ExtendHelper extendHelper;
    @Override
    public void execute(SendMessage message) {
        UserBO userBO = userHelper.checkOpUserAll(message);
        List<String> params = getAllParam(message);

        if (StringUtils.isEmpty(params.get(0))) {
            StringBuffer sb =new StringBuffer();
            sb.append("可以在后面追加 挑战 等二级指令进行后续操作\n")
                    .append("副本共6层，每层的怪物都会更强，掉落概率也会更高，这里的掉落概率不受仙缘影响\n通过副本的6层试炼后，必定获得一份随机材料哦\n")
                    .append("星期一四，二五，三六会分别掉落不同的三种材料，周日全材料掉落");
            messageHelper.replyMys(message, sb.toString());
            return;
        }
        DungeonEnum byMap = DungeonEnum.getByMap(userBO.getLocal());
        if (byMap == null){
            throw new HandlerException("当前仅在千风神殿，渌华池有副本");
        }
        XiuXianExtend byAccountId = extendHelper.getByAccountId(userBO.getId());
        if(byAccountId.getResin() < 1){
            throw new HandlerException("副本开启需要一个原粹树脂");
        }else {
            byAccountId.setResin(byAccountId.getResin() - 1);
            extendHelper.updateExtendById(byAccountId);
        }

        StringBuffer battle = new StringBuffer();
        int days = 0;

        for(int i = 0;i<6;i++){
            //构建怪物对象
            EnemyBO enemyBO = enemyHelper.genDungeonEnemy(byMap, i);
            UserBO enemy = enemyBO.getUserBO();
            //战斗
            BattleContent content = battleEngine.battle(userBO, enemy);
            //战斗时长
            days += battleHelper.calCostTime(content);
            battleHelper.syncUserState(content);

            if (content.getResult() == 1) {
                battle.append("你通过了第").append(i+1).append("层的试炼\n");
                battle.append("当前状态： 血量剩余").append(userBO.getUserBattle().getChp()).append("蓝量剩余").append(userBO.getUserBattle().getCmp()).append("\n");
                //获取材料判定
                double random = Math.random();
                // 0.11 - 0.66
                if(random < (0.1 * i)){
                    Date date = new Date();
                    Calendar calendar =Calendar.getInstance();
                    calendar.setTime(date);
                    //一周中的第几天，7 1-6
                    // 1 2 3 4 5 6 7
                    // 1 2 0 1 2 0 1
                    int weekDay = calendar.get(Calendar.DAY_OF_WEEK);
                    String dropName ;
                    if(weekDay == 1){
                        dropName= RandomUtil.choice(byMap.getDropItem());
                    }else {
                        dropName= byMap.getDropItem().get(weekDay % 3);
                    }
                    itemHelper.addItem(userBO,dropName,1);
                    battle.append("你获得了["+dropName+"]*1\n");
                }
                if( i == 5){
                    String onew = RandomUtil.choice(byMap.getDropItem());
                    itemHelper.addItem(userBO,onew,1);
                    battle.append("你通过了副本的试炼，额外获得了["+onew+"]*1\n");
                }

            } else {
                battle.append("你在第").append(i+1).append("层的试炼倒下了\n");
                break;
            }

        }
        userHelper.lockUser(userBO, days/2, userBO.getName() + "正在副本挑战中");
        messageHelper.replyMys(message,battle.toString());

    }

    public String desc(){
        return new StringBuffer().append("副本挑战的指令，可获得锻造装备所需的必要材料\n")
                .append("二级三级装备所需的武器本材料获取的唯一途径，单次挑战需要一个原粹树脂\n")
                .append("6层副本挑战通过之后，必定会获得一个该副本可获得的随机材料")
                .toString();

    }
}
